package cn.tedu.controller;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//自定义拦截器:把业务代码和业务无关的代码分开
@Component  //交给spring进行ioc
public class MyInterceptor implements HandlerInterceptor {
    long start;

    @Override //在正式调用自己的方法前,都会先触发preHandle
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        start = System.currentTimeMillis() ;//计时开始
        System.out.println("开始准备执行功能~~");
        return true;//放行,继续执行后面的功能
    }
    @Override   //在调用自己的方法结束后,触发postHandle
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long end = System.currentTimeMillis() ;//计时结束
        System.out.println("方法执行完了,耗时: "+(end-start)+"ms");
    }
    @Override //都干完了
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("资源已被释放...");
    }
}